package com.chencs.app.member.service;

import com.chencs.app.AppCommon;
import com.chencs.app.member.mapper.AppMemberMapper;
import com.chencs.utils.StringUtils;
import com.chencs.vo.biz.member.AgentModel;
import com.chencs.vo.biz.member.Agent_Member;
import com.chencs.vo.biz.member.Agent_Order;
import com.chencs.vo.biz.member.MemberModel;
import com.chencs.vo.net.request.AgentRequest;
import com.chencs.vo.net.request.RegisterRequest;
import com.chencs.vo.net.request.RequestBase;
import com.chencs.vo.net.response.ResponseBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by fangzehua on 2017/7/11.
 */
@Service
@Transactional
public class AppMemberService {


    @Autowired
    private AppMemberMapper memberMapper;



    public ResponseBase checkAgent(AgentRequest request){
        ResponseBase response = new ResponseBase();
        AgentModel agentModel= memberMapper.checkAgent(request.getAgent_code());
        if(agentModel==null){
            response.setCode(AppCommon.ERROR);
            response.setMsg("代理码错误");
        }else{
            response.setCode(AppCommon.SUCCESS);
            response.setMsg("绑定成功");
        }
        return response;
    }


    public ResponseBase placeOrder(AgentRequest request){
        ResponseBase response = new ResponseBase();
      try {
          Agent_Order agent_order = new Agent_Order();
          agent_order.setAgent_code(request.getAgent_code());
          agent_order.setOrderno(request.getOrder_no());
          agent_order.setTaobao_id(request.getTaoabo_id());
          agent_order.setPlatform(request.getOs());
          memberMapper.placeOrder(agent_order);
          response.setCode(AppCommon.SUCCESS);
          response.setMsg("订单提交成功!");
      }catch (Exception e){
          response.setCode(AppCommon.ERROR);
          response.setMsg("订单提交失败!");
      }


        return response;
    }

    public ResponseBase activation(AgentRequest request){
        ResponseBase response = new ResponseBase();
        Agent_Member agent_member = new Agent_Member();
        agent_member.setAgent_code(request.getAgent_code());
        agent_member.setDev_id(request.getDevice_id());
        agent_member.setPlatform(request.getOs());
        agent_member.setUptime(StringUtils.getTime());
        memberMapper.activation(agent_member);
        response.setCode(AppCommon.SUCCESS);
        response.setMsg("激活成功!");
        return response;
    }







    public int register(RegisterRequest registerRequest){

        MemberModel memberModel = memberMapper.findMemberBySid(registerRequest.getMember().getTaobaosid());
        MemberModel member = registerRequest.getMember();
        if(memberModel!=null){
            //更新信息
            member.setLastversion(registerRequest.getVersion());
            memberMapper.updateMember(member);
            return 0;
        }else{//添加信息
            memberMapper.insertMember(member);
            return 1;
        }
    }

    public MemberModel findMemberBySid(RequestBase base){

        return  memberMapper.findMemberBySid(base.getSid());

    }

}
